/*
 * Copyright (c) 1994-2017, Joyoung Stock CO.LTD.
 * The FWJR Project.
 * All Rights Reserved.
 */

package jz.joyoung.robot.util;

import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import org.json.JSONArray;
import org.json.JSONException;

/**
 * 配置信息存储帮助类
 */
public class PreferencesHelper {
    private final static String PRE_NAME = "com.joyoung.robot.phone.common";

    private SharedPreferences mConfig = null;

    private PreferencesHelper() {
    }

    private static volatile PreferencesHelper instance = null;

    public static PreferencesHelper getInstance() {
        // if already initialed, no need to get lock every time
        if (instance == null) {
            synchronized (PreferencesHelper.class) {
                if (instance == null) {
                    instance = new PreferencesHelper();
                }
            }
        }

        return instance;
    }

    /**
     * 若不存在Key，则返回 空字符串
     */
    public String getString(Context context, String Key) {
        return getConfig(context).getString(Key, "");
    }

    public String getString(Context context, String Key, String defaultValue) {
        return getConfig(context).getString(Key, defaultValue);
    }

    /**
     * 若不存在Key，则返回0
     */
    public int getInt(Context context, String Key) {
        return getConfig(context).getInt(Key, 0);
    }

    /**
     * 若不存在Key，则返回 defValue
     */
    public int getInt(Context context, String Key, int defValue) {
        return getConfig(context).getInt(Key, defValue);
    }

    /**
     * 若不存在Key，则返回 false
     */
    public boolean getBoolean(Context context, String Key) {
        return getConfig(context).getBoolean(Key, false);
    }

    public boolean getBoolean(Context context, String Key, boolean defValue) {
        return getConfig(context).getBoolean(Key, defValue);
    }

    /**
     * 若不存在Key，则返回 0
     */
    public long getLong(Context context, String Key) {
        return getConfig(context).getLong(Key, 0);
    }

    public long getLong(Context context, String Key, long defValue) {
        return getConfig(context).getLong(Key, defValue);
    }

    public String[] getStringArray(Context context, String key) {
        String jsonArrayStr = getConfig(context).getString(key, "");
        if (TextUtils.isEmpty(jsonArrayStr)) return null;
        try {
            JSONArray jsonArray = new JSONArray(jsonArrayStr);
            String[] strArray = new String[jsonArray.length()];
            for (int i = 0; i < jsonArray.length(); i++) {
                strArray[i] = jsonArray.getString(i);
            }
            return strArray;
        } catch (JSONException e) {
            e.printStackTrace();
            return null;
        }
    }

    public void set(Context context, String key, String value) {
        SharedPreferences.Editor editor = getConfig(context).edit();
        editor.putString(key, value);
        editor.apply();
    }

    public void set(Context context, String key, int value) {
        SharedPreferences.Editor editor = getConfig(context).edit();
        editor.putInt(key, value);
        editor.apply();
    }

    public void set(Context context, String key, boolean value) {
        SharedPreferences.Editor editor = getConfig(context).edit();
        editor.putBoolean(key, value);
        editor.apply();
    }

    public void set(Context context, String key, long value) {
        SharedPreferences.Editor editor = getConfig(context).edit();
        editor.putLong(key, value);
        editor.apply();
    }

    public void set(Context context, String key, String[] value) {
        JSONArray jsonArray = new JSONArray();
        for (String str : value) {
            jsonArray.put(str);
        }
        SharedPreferences.Editor editor = getConfig(context).edit();
        editor.putString(key, jsonArray.toString());
        editor.apply();
    }

    /**
     * 添加到字符串数组，不存在则创建
     */
    public void add2StrArray(Context context, String key, String value) {
        String jsonArrayStr = getConfig(context).getString(key, "");
        JSONArray jsonArray;
        try {
            jsonArray = new JSONArray(jsonArrayStr);
        } catch (JSONException e) {
            e.printStackTrace();
            jsonArray = new JSONArray();
        }
        jsonArray.put(value);
        SharedPreferences.Editor editor = getConfig(context).edit();
        editor.putString(key, jsonArray.toString());
        editor.apply();
    }

    /**
     * 是否存在key值
     */
    public boolean contains(Context context, String key) {
        return getConfig(context).contains(key);
    }

    private SharedPreferences getConfig(Context context) {
        if (mConfig == null) {
            mConfig = context.getApplicationContext().getSharedPreferences(PRE_NAME, Context.MODE_PRIVATE);
        }
        return mConfig;
    }

}
